<?php
	class UserModel extends CI_Model{

		function __construct() {
			parent::__construct ();
			$this->load->database();
		}


		function validate($data){
			$query = $this->db->get('user');
			//print_r ($query->result_array());
			$this->db->where('username',$data['username']);

			$password = md5($data['password']);
			//echo $password;
			$this->db->where('password',$password);
			$query = $this->db->get('user');

			if($query->num_rows () == 1)
			{
				return true;
			}
			else {
				return false;
			}
		}

		function isExist(){
			$this->db->where ( 'username =', $this->input->post('username') );
			$this->db->or_where ( 'email =', $this->input->post('email') );
			$query = $this->db->get ( 'user' );
			return ($query->num_rows () > 0);
		}

		function deleteUser($id) {
			$this->db->where ( 'id', $id );
			$query = $this->db->delete ( 'user' );
			if (! $query)
				return false;
			else
				return true;
		}
		
		function getUser($id = 0){
			if($id===0){
				$query = $this->db->get('user');
				return $query->result_array();
			}
			else{
				$this->db->where('id',$id);
				$query = $this->db->get('user');
				if($query->num_rows() > 0)
					return $query->first_row();
			}
		}

		function updateUser($id,$data){
			$this->db->where('id',$id);
			return $this->db->update ( 'user', $data );
		}
		
		function addUser($data){
			return $this->db->insert('user',$data);
		}

	}	
?>